﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace Lasalara.View
{
    public partial class Sponsor
    {
        public Sponsor()
        {
            InitializeComponent();
            Loaded += SponsorLoaded;
        }

        protected override void OnNavigatedTo(NavigationEventArgs navigationEventArgs)
        {
        }

        private void SponsorLoaded(object sender, RoutedEventArgs e)
        {
            var uri = new Uri("http://lasalara.googlecode.com/svn/trunk/Lasalara/Images/nikebanner.png", UriKind.Absolute);
            if (NavigationContext.QueryString.ContainsKey("chaptersPivotItem"))
            {
                uri = new Uri("http://lasalara.googlecode.com/svn/trunk/Lasalara/Images/nikebanner2.png", UriKind.Absolute);
            }
            if (NavigationContext.QueryString.ContainsKey("studyPivotItem"))
            {
                uri = new Uri("http://lasalara.googlecode.com/svn/trunk/Lasalara/Images/nikebanner3.png", UriKind.Absolute);
            }
            ImageSource imgSource = new BitmapImage(uri);
            sponsorPicture.Source = imgSource;
            sponsorPicture.Tap += SponsorPictureTap;
            ShowHide(LayoutRoot, null, RenderSize.Height, 0, 0, 1, 1);
        }

        static void SponsorPictureTap(object sender, EventArgs e)
        {
            var wbt = new WebBrowserTask {Uri = new Uri(AppResources.SponsorUri, UriKind.Absolute)};
            wbt.Show();
        }

        /// <summary>
        /// Back button event handler, with animation out.
        /// </summary>
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            e.Cancel = true;
            if (NavigationContext.QueryString.ContainsKey("myBooksPivotItem"))
            {
                NavigationService.Navigate(new Uri("/View/Books.xaml?fromSponsor", UriKind.Relative));
            }
            if (NavigationContext.QueryString.ContainsKey("chaptersPivotItem"))
            {
                // Mimic the behaviour when leaving the study view.
                NavigationService.Navigate(new Uri("/View/Books.xaml?fromStudyView", UriKind.Relative));
            }
            if (NavigationContext.QueryString.ContainsKey("changePasswordPivotItem"))
            {
                NavigationService.Navigate(new Uri("/View/Books.xaml?fromSponsor&toChangePassword", UriKind.Relative));
            }
            if (NavigationContext.QueryString.ContainsKey("addBooksPivotItem"))
            {
                NavigationService.Navigate(new Uri("/View/Books.xaml?fromSponsor&toAddBook", UriKind.Relative));
            }
            if (NavigationContext.QueryString.ContainsKey("studyPivotItem"))
            {
                NavigationService.Navigate(new Uri("/View/Study.xaml", UriKind.Relative));
            }
            if (NavigationContext.QueryString.ContainsKey("pageViewPivotItem"))
            {
                NavigationService.Navigate(new Uri("/View/Study.xaml?fromSponsor&toPageView", UriKind.Relative));
            }
            NavigationContext.QueryString.Clear();
            NavigationService.RemoveBackEntry();
            ShowHide(LayoutRoot, null,
                0, RenderSize.Height, 1, 0, 0.3);

        }
        #region animation onLoad
        private static void ShowHide(UIElement item, Action callback, double topFrom,
    double topTo, double opacityFrom, double opacityTo, double time)
        {
            // setup
            var translate = new TranslateTransform
            {
                X = 0,
                Y = topFrom
            };
            item.RenderTransform = translate;
            item.Visibility = Visibility.Visible;
            item.Opacity = opacityFrom;

            // animate
            var storyboard = new Storyboard();
            var duration = TimeSpan.FromSeconds(time);

            // opacity
            var opacityAnimate = new DoubleAnimation
            {
                To = opacityTo,
                Duration = duration,
            };
            storyboard.Children.Add(opacityAnimate);
            Storyboard.SetTarget(opacityAnimate, item);
            Storyboard.SetTargetProperty(opacityAnimate,
                new PropertyPath(UIElement.OpacityProperty));

            // translate (location)
            var translateAnimate = new DoubleAnimation
            {
                To = topTo,
                Duration = duration,
                EasingFunction = new SineEase
                {
                    EasingMode = EasingMode.EaseOut
                }
            };
            storyboard.Children.Add(translateAnimate);
            Storyboard.SetTarget(translateAnimate, translate);
            Storyboard.SetTargetProperty(translateAnimate,
                new PropertyPath(TranslateTransform.YProperty));

            // finalize
            translateAnimate.Completed += (s, arg) =>
            {
                if (callback != null)
                    callback();
            };
            storyboard.Begin();
        }
        #endregion


    }
}